import axios from 'axios'
import Cookie from 'js-cookie'
import Vue from 'vue'

let toke = Cookie.get('token')?Cookie.get('token'):''
console.log(toke)
const http = axios.create({
  baseURL: '/api',
  //baseURL: 'https://www.zhongchuangmiao.com/back',
  headers:{'token': toke}
})

if(process.browser){
  // axios.post( 'http://121.199.40.110:8077/back/users/verifyToken',{token:Cookie.get("token")}).then((res)=>{
  //   return
  //   if(res.data.code == 500301){
  //     Cookie.set('token','')
  //     Vue.prototype.islogin = false
  //     if(location.pathname!='/login'){
  //       //location.href='/login'
  //     }
  //   }else{
  //     Vue.prototype.islogin = true
  //     // if(location.pathname=='/'){
  //     //   location.href='/user/data'
  //     // }
  //   }
  // })
}


function apiAxios(method, url, params, response) {
  params.token = Cookie.get("token")
  http({
    method: method,
    url: url,
    timeout: 300000,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null
  })
    .then(function(res) {
      if(res.data && res.data.code == 500301){

      }else{
        response(res.data.data,res.data)
      }
    }).catch(function() {
    response({data:null})
  })
}

Vue.prototype.$get = (url, params, response) => {
  return apiAxios('GET', url, params, response)
}

Vue.prototype.$post = (url, params, response) => {
  return apiAxios('POST', url, params, response)
}
